import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MessageService } from 'src/app/service/message.service';
import { UserBindService } from 'src/app/service/user-bind.service';
import { UserInfoService } from 'src/app/service/user-info/user-info.service';

@Component({
  selector: 'app-user-bind-list',
  templateUrl: './user-bind-list.component.html',
  styleUrls: ['./user-bind-list.component.scss']
})
export class UserBindListComponent implements OnInit {
  state = {
    '0' : { value: '申请', color: 'orange' }, 
    '1' : { value: '认证通过', color: 'green' }, 
    '2' : { value: '认证不通过', color: 'red' }, 
    '3' : { value: '解除绑定', color: 'gray' }, 
  };
  constructor(
    private router: Router,
    private userInfo: UserInfoService,
    private message: MessageService,
    private userBind: UserBindService
  ) { }
  	
  cols = {
    'usageCode': '用户编号',
    'name': '户名',
    'address': '用水地址',
    'type': '状态',
    '操作': '操作',
  }
  data = []
  dataLoading = false
  total = 0
  pageSize = 10
  pageIndex = 1
  ngOnInit(): void {
    if(this.userInfo.getUserInfo()?.phone){
      this.getUserBindList()
    }else{
      this.message.infoModal('请先登录', () => {
        this.router.navigate(['/login'], { queryParams: { redirect_url: '/me/user-bind/list' } })
      })
    }
  }
  getUserBindList(){
    this.dataLoading = true
    this.userBind._getUserBindList({
      phone: this.userInfo.getUserInfo()?.phone,
      pageNumber: this.pageIndex,
      pageSize: this.pageSize
    }).subscribe({
      next: (res: any) => {
        if(res && res.resultList){
          this.data = res.resultList
          this.total = res.totalNumber
        }
        this.dataLoading = false
      },
      error: error => {
        this.message.error('查询数据错误, 请稍后再试')
        this.dataLoading = false
      }
    })
  }

  add(){
    this.router.navigate(['/me/user-bind/add'])
  }
  unBind(data: any){
    this.userBind._userUnbind(data.userBindId).subscribe({
      next: (res) => {
        if(res && res.result === 'success'){
          this.message.success('解绑成功')
          this.pageIndex = 1
        }else{
          this.message.error('解绑失败')
        }
        this.getUserBindList()
      },
      error: error => {
        this.message.error('解绑失败')
      }
    })
  }

}
